package com.digitalchina.lxnsy.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.PestDetailActivity;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PestListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.viewholder.PestListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2019/4/17 11:36
 * 修改备注：
 * ================================================
 */
public class PestFragment extends BaseFragment {
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;

    private String typeCode = ""; // 病虫草害类型code 病害-bcch_bh  虫害-bcch_ch
    private String farmOperationId = ""; // 农事操作id
    private String productStandardId = ""; // 生产标准id

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_pest);
    }

    @Override
    public void initMember() {

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        //recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1, 0, 0));
        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new PestListHolder(parent);
            }
        });

//        gridLayoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(3));


        if (getArguments() != null) {
            typeCode = getArguments().getString("typeCode");
            farmOperationId = getArguments().getString("farmOperationId");
            productStandardId = getArguments().getString("productStandardId");
        }

        BusinessAgr.getPestList(getActivity(), typeCode, farmOperationId, productStandardId, mHandler);
    }

    @Override
    public void addListener() {
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                GotoUtil.gotoActivity(getActivity(), PestDetailActivity.class, "PestListVO", adapter.getAllData().get(position));
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.MSG_GET_PEST_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PEST_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        List<PestListVO> list = FastJsonUtil.getListBean(object.toString(), "diseasePestList", PestListVO.class);
        adapter.clear();
        adapter.addAll(list);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
